<?php

namespace app\index\controller;

use app\common\lib\Util;
use app\common\lib\Redis;
use app\common\lib\redis\Predis;

class Login
{
    public function index()
    {
        //redis.so，同步的redis，这里不需要异步
        //获取手机号，验证码
        $phoneNum = intval($_GET['phone_num']);
        $code = intval($_GET['code']);
        if (empty($phoneNum) || empty($code)) {
            return Util::show(config('code.error'), 'phone or code error');
        }
        $redisCode = '';
        //redis去对比
        try {
            $redisCode = Predis::getInstance()->get(Redis::smsKey($phoneNum));
        } catch (\Exception $e) {
            echo $e->getMessage();
        }

        //判断，验证码正确，做登陆动作
        if ($redisCode == $code) {
            //登陆成功
            $data = [
                'user' => $phoneNum,
                'srcKey' => md5(Redis::userKey($phoneNum)),
                'time' => time(),
                'isLogin' => 'true',
                'status' => 1,
            ];
            //把数据存到redis
            Predis::getInstance()->set(Redis::userKey($phoneNum), $data);

            //登陆成功后，我们要清楚redis,避免别人再用这个code登陆
            Predis::getInstance()->del(Redis::smsKey($phoneNum));
            return Util::show(config('code.success'), 'ok', $data);
        } else {
            //验证码错误，返回登陆页面
            return Util::show(config('code.error'), 'login error!!');
        }
    }

    public function showList()
    {

        var_dump(123);
        return;
    }
}
